import QtQuick 2.8
import QtQuick.Controls 2.1

Item {
    id: root
    implicitWidth: 280
    implicitHeight: 30

    property bool isWindow: true
    property alias title: label.text
    default property alias contentItem: buttons.children

    MouseArea {
        anchors.fill: parent
        property point clickPos: "0,0"
        onPressed: {
            clickPos  = Qt.point(mouse.x, mouse.y);
        }
        onPositionChanged: {
            var mainWindow = ApplicationWindow.window;
            var delta = Qt.point(mouse.x - clickPos.x, mouse.y - clickPos.y);
            mainWindow.setX(mainWindow.x + delta.x);
            mainWindow.setY(mainWindow.y + delta.y);
        }
    }

    Label {
        id: label
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.verticalCenter: parent.verticalCenter
        color: "#A7A6AA"
        font.pixelSize: 14
    }

    Row {
        id: buttons
        anchors.right: titles.left
    }

    Row {
        id: titles
        anchors.right: parent.right

        TitleButton {
            visible: root.isWindow
            source: "qrc:/images/btn_title_minimize.png"
            onClicked: ApplicationWindow.window.showMinimized()
        }

        TitleButton {
            visible: root.isWindow && ApplicationWindow.window.visibility !== ApplicationWindow.Maximized
            source: "qrc:/images/btn_title_maximize.png"
            onClicked: ApplicationWindow.window.showMaximized()
        }

        TitleButton {
            visible: root.isWindow && ApplicationWindow.window.visibility === ApplicationWindow.Maximized
            source: "qrc:/images/btn_title_normal.png"
            onClicked: ApplicationWindow.window.showNormal()
        }

        TitleButton {
            isCloseButton: true
            source: "qrc:/images/btn_title_close.png"
            onClicked: ApplicationWindow.window.close()
        }
    }
}
